package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpCustomerIntegral;
import com.monitor.service.ErpCustomerIntegralService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 客户积分表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-06
 */
@RestController
@RequestMapping("/erpCustomerIntegral")
@Api(description="客户积分")
public class ErpCustomerIntegralController {

    @Resource
    private ErpCustomerIntegralService integralService;
    

    @RequestMapping("/inertOrUpdateCustomerIntegral")
    @ApiOperation("添加Or修改客户积分信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpCustomerIntegral> inertOrUpdateCustomerIntegral(ErpCustomerIntegral erpCustomerIntegral) {
        Result<ErpCustomerIntegral> result=new Result<ErpCustomerIntegral>();
        try{
        	 ErpCustomerIntegral erpCustomerIntegral1 = integralService.selectOne(new EntityWrapper<ErpCustomerIntegral>().eq("customer_id", erpCustomerIntegral.getCustomerId()));
             if (null==erpCustomerIntegral1){  
                 integralService.insert(erpCustomerIntegral);
             }else {
            	 erpCustomerIntegral.setId(erpCustomerIntegral1.getId());
            	 integralService.updateById(erpCustomerIntegral);
             }
             
            result.setCount(1);
            result.setData(erpCustomerIntegral);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getcustIntegralByCustId")
    @ApiOperation("根据客户id客户积分信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpCustomerIntegral> inertOrUpdateCustomerCar(Integer custId) {
        Result<ErpCustomerIntegral> result=new Result<ErpCustomerIntegral>();
        try{

            ErpCustomerIntegral customerCar=integralService.selectOne(new EntityWrapper<ErpCustomerIntegral>().eq("customer_id",custId));
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeCustomerIntegral")
    @ApiOperation("删除客户积分信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "customerCarId", value = "客户积分ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeCustomerIntegral(Integer customerCarId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(integralService.deleteById(customerCarId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getIntegralBycustIdPage")
    @ApiOperation("根据客户ID查询客户积分信息分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpCustomerIntegral>> getIntegralBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                            @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<Page<ErpCustomerIntegral>> result=new Result<Page<ErpCustomerIntegral>>();
        try{
            Page<ErpCustomerIntegral> page1=new Page<ErpCustomerIntegral>(page,limit);
            Page<ErpCustomerIntegral> carPage=integralService.selectPage(page1,new EntityWrapper<ErpCustomerIntegral>().eq("customer_id",custId));
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }



}

